package com.ycd.dao.Impl;

import com.ycd.dao.LanMuEntityDao;
import com.ycd.Entity.LanMuEntity;
import com.ycd.Util.jdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 栏目的实现类
 * @Author ycd
 * @Date : 2022/3/13 15:29
 * @Version : 1.0
 */
public class LanMuEntityImpl implements LanMuEntityDao {

    /**
     * 添加栏目
     * @param lanmuentity  栏目实现
     */
    @Override
    public void addLanMu(LanMuEntity lanmuentity) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "insert into news_chnl (Name,sort,crtime,uptime) values(?,?,now(),now())";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, lanmuentity.getName());
            stmt.setInt(2, lanmuentity.getSort());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, null);
        }
    }


    /**
     * 修改栏目
     * @param lanmuentity
     */
    @Override
    public void updateLanMu(LanMuEntity lanmuentity) {

        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = " update news_chnl set Name=? where id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, lanmuentity.getName());
            stmt.setInt(2, lanmuentity.getId());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, null);
        }
    }

    /**
     * 删除栏目
     * @param id
     */
    @Override
    public void deLanMu(int id) {
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "delete from news_chnl where id=?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            stmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtil.closeAll(conn,stmt,null);
        }

    }

    /**
     * 查询全部栏目
     * @return
     */
    @Override
    public List<LanMuEntity> queryAll() {
        List<LanMuEntity> list = new ArrayList<>();
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "select id, Name,sort,crtime,uptime from news_chnl ";
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                LanMuEntity lanmuEntity=new LanMuEntity();

                lanmuEntity.setId(rs.getInt("id"));
                lanmuEntity.setName(rs.getString("Name"));
                lanmuEntity.setSort(rs.getInt("sort"));
                lanmuEntity.setCrtime(rs.getDate("crtime"));
                lanmuEntity.setUptime(rs.getDate("uptime"));

                list.add(lanmuEntity);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, rs);
        }
        return list;
    }

    /**
     * ID查栏目
     * @param id
     * @return
     */
    @Override
    public LanMuEntity getUserById(int id) {
        LanMuEntity lanmuentity=null;
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try {
            conn = jdbcUtil.getCon();
            String sql = "select * from news_chnl where id=?";
            stmt = conn.prepareStatement(sql);

            stmt.setInt(1,id);
            rs = stmt.executeQuery();
            while(rs.next()) {
                lanmuentity=new LanMuEntity();
                lanmuentity.setId(rs.getInt("id"));
                lanmuentity.setName(rs.getString("Name"));
                lanmuentity.setSort(rs.getInt("sort"));
                lanmuentity.setCrtime(rs.getDate("crtime"));
                lanmuentity.setUptime(rs.getDate("uptime"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeAll(conn, stmt, rs);
        }
        return lanmuentity;
    }

    @Override
    public boolean getAddReg(String acct) {
        return false;
    }

    @Override
    public List<Map<String, String>> queryMu() {
        return null;
    }

}
